home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifTextUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  144 lines

  1. /*
  2.  * @(#)MotifTextUI.java    1.10 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.plaf.motif;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.KeyEvent;
  24. import java.awt.event.InputEvent;
  25.  
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.text.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * Provides the look and feel features that are common across
  32.  * the Motif/CDE text LAF implementations.  
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @author  Timothy Prinzing
  42.  * @version 1.10 04/09/98
  43.  */
  44. public class MotifTextUI {
  45.  
  46.     /**
  47.      * Creates the object to use for a caret for all of the Motif
  48.      * text components.  The caret is rendered as an I-beam on Motif.
  49.      *
  50.      * @return the caret object
  51.      */
  52.     public static Caret createCaret() {
  53.     return new MotifCaret();
  54.     }
  55.  
  56.     /**
  57.      * The motif caret is rendered as an I beam.
  58.      * <p>
  59.      * Warning: serialized objects of this class will not be compatible with
  60.      * future swing releases.  The current serialization support is appropriate
  61.      * for short term storage or RMI between Swing1.0 applications.  It will
  62.      * not be possible to load serialized Swing1.0 objects with future releases
  63.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  64.      * baseline for the serialized form of Swing objects.
  65.      */
  66.     public static class MotifCaret extends DefaultCaret implements UIResource {
  67.  
  68.     /**
  69.      * Damages the area surrounding the caret to cause
  70.      * it to be repainted.  If paint() is reimplemented,
  71.      * this method should also be reimplemented.
  72.      *
  73.      * @param r  the current location of the caret, does nothing if null
  74.      * @see #paint
  75.      */
  76.         protected void damage(Rectangle r) {
  77.         if (r != null) {
  78.         Component c = getComponent();
  79.         c.repaint(r.x - IBeamOverhang - 1, r.y, 
  80.               r.width + (2 * IBeamOverhang) + 3, r.height);
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Renders the caret as a vertical line.  If this is reimplemented
  86.      * the damage method should also be reimplemented as it assumes the
  87.      * shape of the caret is a vertical line.  Does nothing if isVisible()
  88.          * is false.  The caret color is derived from getCaretColor() if
  89.          * the component has focus, else from getDisabledTextColor().
  90.      *
  91.      * @param g the graphics context
  92.      * @see #damage
  93.      */
  94.         public void paint(Graphics g) {
  95.         if(isVisible()) {
  96.         try {
  97.             JTextComponent c = getComponent();
  98.             Color fg = c.hasFocus() ? c.getCaretColor() : 
  99.             c.getDisabledTextColor();
  100.             TextUI mapper = c.getUI();
  101.             int dot = getDot();
  102.             Rectangle r = mapper.modelToView(dot);
  103.             int x0 = r.x - IBeamOverhang;
  104.             int x1 = r.x + IBeamOverhang;
  105.             int y0 = r.y + 1;
  106.             int y1 = r.y + r.height - 2;
  107.             g.setColor(fg);
  108.             g.drawLine(r.x, y0, r.x, y1);
  109.             g.drawLine(x0, y0, x1, y0);
  110.             g.drawLine(x0, y1, x1, y1);
  111.         } catch (BadLocationException e) {
  112.             // can't render I guess
  113.             //System.err.println("Can't render caret");
  114.         }
  115.         }
  116.     }
  117.     
  118.     static final int IBeamOverhang = 2;
  119.     }
  120.  
  121.     /**
  122.      * Default bindings all keymaps implementing the Motif feel.
  123.      */
  124.     static final JTextComponent.KeyBinding[] defaultBindings = {
  125.     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 
  126.                                     InputEvent.CTRL_MASK),
  127.                          DefaultEditorKit.copyAction),
  128.     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 
  129.                                     InputEvent.SHIFT_MASK),
  130.                          DefaultEditorKit.pasteAction),
  131.     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 
  132.                                     InputEvent.SHIFT_MASK),
  133.                          DefaultEditorKit.cutAction),
  134.     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 
  135.                                     InputEvent.SHIFT_MASK),
  136.                          DefaultEditorKit.selectionBackwardAction),
  137.     new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 
  138.                                     InputEvent.SHIFT_MASK),
  139.                          DefaultEditorKit.selectionForwardAction),
  140.     };
  141.  
  142.  
  143. }
  144.